package com.example.day2_popupwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActionBar;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

//弹出窗体和对话框的区别：对话框屏幕中间 弹出窗体手机底部
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showwindow();
            }
        });

    }

    private void showwindow() {
        //TODO 1:创建对象
        final PopupWindow popupWindow = new PopupWindow(this);
        //TODO 2：设置属性:三大要素,必须设置
        View view = LayoutInflater.from(this).inflate(R.layout.popup, null);
        popupWindow.setContentView(view);//设置布局样式
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //****窗体控件的点击
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();//关闭窗体
            }
        });
        //******弹出窗体背景半透明
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.5f;
        getWindow().setAttributes(attributes);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {//当窗体消失的时候
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getWindow().getAttributes();
                attributes.alpha = 1f;
                getWindow().setAttributes(attributes);
            }
        });
        //******设置点击外部关闭窗体
        popupWindow.setOutsideTouchable(true);
        //******动态设置背景：左上 右上是圆角 结合xml文件 shape
        popupWindow.setBackgroundDrawable(getDrawable(R.drawable.bg));
        //*****设置动画
        popupWindow.setAnimationStyle(R.style.pop);
        //TODO 3：显示窗体
        View parent = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);

    }
}
